﻿
namespace TransportAnalizer_2._0.Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Encapsulates business rules when accessing the data layer.
    /// </summary>
    public sealed class NodeTypeBusinessContext : IDisposable, TransportAnalizer_2._0.Model.INodeTypeBusinessContext
    {
        private TransportAnalizerModelContext context;
        private bool disposed;

        /// <summary>
        /// Initializes a new instance of the <see cref="NodeTypeBusinessContext"/> class.
        /// </summary>
        public NodeTypeBusinessContext()
        {
            context = new TransportAnalizerModelContext();
        }

        /// <summary>
        /// Gets the underlying <see cref="DataContext"/>.
        /// </summary>
        public TransportAnalizerModelContext DataContext
        {
            get { return context; }
            set { context = value; }
        }

        #region NodeTypeBusinessContext
       /// <summary>
        /// Adds a new nodeType entity to the data store.
       /// </summary>
       /// <param name="nodeType"></param>
        public void CreateNodeType(NodeType nodeType)
        {
                Check.RequireNotNullOrEmpty(nodeType.Name);
                Check.RequireNotNull(nodeType.Description);
                context.NodeTypeSet.Add(nodeType);
                context.SaveChanges();
        }

        /// <summary>
        /// Updates the specified nodeType by applying the values passed in over the existing values from the data store.
        /// </summary>
        /// <param name="nodeType">The nodeType entity containing the new values to persist.</param>
        public void UpdateNodeType(NodeType nodeType)
        {
            var entity = context.NodeTypeSet.Find(nodeType.Id);

            if (entity == null)
            {
                throw new NotImplementedException("Handle appropriately for your API design.");
            }

            context.Entry(nodeType).CurrentValues.SetValues(nodeType);
            context.SaveChanges();
        }

        /// <summary>
        /// Deletes the specified noteType.
        /// </summary>
        /// <param name="nodeType">The nodeType to delete.</param>
        public void DeleteNodeType(NodeType nodeType)
        {
            context.NodeTypeSet.Remove(nodeType);
            context.SaveChanges();
        }


        /// <summary>
        /// Gets a collection of NodeTypes.
        /// </summary>
        /// <returns>Returns a collection of <see cref="NodeType"/> entities ordered by primary key.</returns>
        public ICollection<NodeType> GetNodeTypeList()
        {
            return context.NodeTypeSet.OrderBy(p => p.Id).ToArray();
        }




        #endregion NodeTypeBusinessContext

       
       

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposed || !disposing)
                return;

            if (context != null)
                context.Dispose();

            disposed = true;
        }

        #endregion
    }
}
